package com.fxwater.websocket.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @Description:
 * @Author: 王明朝
 * @Date: 2019/8/17 13:42
 */
public class KeyUtil {

    public static String getRedisKey(String redisPrefix,String groupId,String keyId,String[] sceneParams){
        String redisKey = "";
        if(redisPrefix.contains("_")){
            String[] prefixGroups = redisPrefix.split("_");
            for(int i=0;i<prefixGroups.length;i++){
                if(i>0){
                    redisKey += "_";
                }
                String prefix = prefixGroups[i];
                if(prefix.contains(".")){
                    String[] prefixs = prefix.split("\\.");
                    String paramName  = prefixs[0];
                    int paramIndex = Integer.parseInt(prefixs[1]);
                    if(paramName.startsWith("$")){
                        paramName = paramName.substring(1);
                        if("SCENIID".equals(paramName)){
                            String paramValue = sceneParams[paramIndex];
                            redisKey += paramValue;
                        }
                    }
                } else if(prefix.startsWith("$")) {
                    String paramName = prefix.substring(1);
                    if("DATE".equals(paramName)){
                        String date = DateUtil.parse(DateUtil.today()).toString("yyyyMMdd") ;
                        redisKey += date;
                    }
                } else {
                    redisKey += prefix;
                }
            }
        } else {
            redisKey += redisPrefix;
        }
        if(StrUtil.isNotEmpty(groupId)){
            redisKey += "_"+groupId;
        }

        if(StrUtil.isNotEmpty(keyId)){
            redisKey += "_"+keyId;
        }
        return redisKey;
    }

    public static String getRedisKey(String sceneId,String groupId,String keyId){
        String redisKey = sceneId;
        if(StrUtil.isNotEmpty(groupId)){
            redisKey += "_"+groupId;
        }

        if(StrUtil.isNotEmpty(keyId)){
            redisKey += "_"+keyId;
        }
        return redisKey;
    }
}
